package 算法.NiuKe.JZ;

import 算法.NiuKe.TreeNode;

import java.util.*;

/**
 * @author:谢君臣
 * @Date:2021/4/289:37
 * @version:1.0
 * @function: 从上到下按层打印二叉树，同一层结点从左至右输出。每一层输出一行。
 */
public class JZ61 {
    ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        if (pRoot == null)
            return res;
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(pRoot);
        while (!queue.isEmpty()) {
            ArrayList<Integer> list = new ArrayList<>();
            Queue<TreeNode> temp = new ArrayDeque<>();
            while (!queue.isEmpty()) {
                TreeNode pop = queue.poll();
                if (pop.left != null) temp.add(pop.left);
                if (pop.right != null) temp.add(pop.right);
                list.add(pop.val);
            }
            queue = temp;
            res.add(list);
        }
        return res;
    }
}
